const express = require("express")
const https = require("https")
var fs = require('fs');
var path = require('path');
const app = express()
const rateLimit = require("express-rate-limit");
 
// Enable if you're behind a reverse proxy (Heroku, Bluemix, AWS ELB, Nginx, etc)
// see https://expressjs.com/en/guide/behind-proxies.html
// app.set('trust proxy', 1);
 
const limiter = rateLimit({
  windowMs: 10 * 60 * 1000, // 15 minutes
  max: 1000 // limit each IP to 100 requests per windowMs
});
 
//  apply to all requests
app.use(limiter);
app.use(require('cors')())
app.use(express.json())
app.use('/',express.static(__dirname + '/public'))
app.use('/uploads',express.static(__dirname + '/uploads'))

//token密钥
app.set('secret','1224234asrt')//加密密钥
app.set('admin',['站长','admin','江南土拨鼠'])//管理员用户名

require('./plugins/db')(app)
require('./routes/upload')(app)


require('./routes/articles')(app)
require('./routes/login')(app)

//错误处理
app.use(async (err,req,res,next)=>{
  res.status(err.statusCode || 500).send({
    message:err.message
  })
})
var privateCrt = fs.readFileSync('./Nginx/1_itying.club_bundle.crt', 'utf8');
var privateKey = fs.readFileSync('./Nginx/2_itying.club.key', 'utf8');
const HTTPS_OPTOIN = {
  key: privateKey,
  cert: privateCrt
};
const SSL_PORT = 3001;
const httpsServer = https.createServer(HTTPS_OPTOIN, app);
httpsServer.listen(SSL_PORT, () => {
  console.log(`HTTPS Server is running on: https://localhost:${SSL_PORT}`);
});
app.listen(3000,()=>{
  console.log(3000)
})